package com.tothefor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tothefor.mapper.UserMapper;
import com.tothefor.mapper.UserRoleMapper;
import com.tothefor.pojo.dto.GroupInfoDTO;
import com.tothefor.pojo.dto.UserDTO;
import com.tothefor.pojo.entity.*;
import com.tothefor.resultR.R;
import com.tothefor.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/userManage")
public class UserManageController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @PostMapping("/userAll")
    public R groupAll(){
        List<User> users=userService.list();

        Page<User> userPage = new Page<>(1, 2);

        userService.page(userPage,null);

        List<User> result=userPage.getRecords();

        System.out.println(result);

        List<User> users1=new ArrayList<>();

        for(User user:users){
            if(user.getRoleId()==4){
                users1.add(user);
            }
        }

        if(users1.size()!=0){
            UserDTO userDTO=new UserDTO(users1);
            return R.SUCCESS(userDTO);
        }else {
            return R.FAIL();
        }

    }

    @DeleteMapping("/del/{id}")
    public R itemDelete(@PathVariable Integer id){
        /*User user=userMapper.selectById(id);
        int isDeleted=0;
        if(user.getGroupid()==0){
            isDeleted=userMapper.deleteById(id);
            return R.SUCCESS();
        }else {
            return R.FAIL();
        }*/
        if(userMapper.deleteById(id)==1){
            return R.SUCCESS();
        }else{
            return R.FAIL();
        }
    }

    @PostMapping("/save")
    public R userSave(@RequestBody User user){
        if(userService.saveOrUpdate(user)){
            return R.SUCCESS();
        }else {
            return R.FAIL();
        }
    }
}
